﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.IO;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using H = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using rz = functionService.serfun_city7.rz;
using nameCod = functionService.nameCod;
namespace city7
{
    public partial class FormPFPF : Form
    {
        functionService.prm_city q;
        rzpt.modeWAED currentMode;
        DataTable dts;

        public FormPFPF()
        {
            InitializeComponent(); InitializeComponentMy();
        }
        private void InitializeComponentMy()
        {
            q = new functionService.prm_city();

            string s = q.tk((int)ETALON.param.server_1);
            q.con.co(s);
            q.UpdateData(rz.sql.add_list_sc_tehsir_z_newCodSc()); 
            refresh();
            set();
            setMode(city7.rzpt.modeWAED.work);

        }
        void refresh()
        {
            dts = q.selectAny("call `city`.`select_list_sc_tehsir_z`();");
            dataGridView1.DataSource = dts;
        }
        void refresh(string whereName)
        {
            if (whereName != null)
            {
                if (whereName.Length > 0)
                {
                    dts = q.selectAny(string.Concat("call `city`.`select_list_sc_tehsir_zW`('", whereName, "');"));
                    dataGridView1.DataSource = dts;
                }
                else
                {
                    dts = q.selectAny("call `city`.`select_list_sc_tehsir_z`();");
                    dataGridView1.DataSource = dts;
                }
            }
        }
        void set()
        {


            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.LightGray;
            dataGridView1.Columns[1].DefaultCellStyle.BackColor = Color.LightGray;

            dataGridView1.Columns[0].Width = 300;
            dataGridView1.Columns[1].Width = 300;

            dataGridView1.Columns[0].HeaderText = "Наименование ПФ прод";
            dataGridView1.Columns[1].HeaderText = "Наименование ПФ сырье";
        }

        void setColor(Color c1, Color c2)
        {
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = c1;
            dataGridView1.Columns[1].DefaultCellStyle.BackColor = c2;
        }
        void setMode(rzpt.modeWAED mo)
        {
            currentMode = mo;
            if (mo == city7.rzpt.modeWAED.work)
            {
                setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                refresh();
            }
            else if (mo == city7.rzpt.modeWAED.add)
            {
                setColor(Color.SteelBlue, Color.LightSkyBlue);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

               // cellclick_add();
            }
            else if (mo == city7.rzpt.modeWAED.edit)
            {
                setColor(Color.Green, Color.LightGreen);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            }
            else if (mo == city7.rzpt.modeWAED.delete)
            {
                setColor(Color.Coral, Color.Coral);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            else if (mo == city7.rzpt.modeWAED.non)
            {
                setColor(Color.LightGray, Color.White);
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }

        }

        void cellclick()
        {
            if (currentMode == city7.rzpt.modeWAED.add)
            {
                cellclick_add();
            }
            else if (currentMode == city7.rzpt.modeWAED.edit)
            {
                cellclick_edit();
            }
            else if (currentMode == city7.rzpt.modeWAED.delete)
            {
                cellclick_del();
            }
            else if (currentMode == city7.rzpt.modeWAED.work)
            {
                cellclick_work();
            }
            else if (currentMode == city7.rzpt.modeWAED.non)
            {
                cellclick_non();
            }
        }

        private string SELECTprod
        {
            get
            {
                string s = "- (888)";
               
                    ETALON.listAnyEdit f =
                        new ETALON.listAnyEdit("city", "list_sc", "cod<888"); f.ShowDialog();
                    if (f.IsOK)
                    {
                        s = f.sText; vv.rz.add_prod_default = s; toolStripStatusLabel1.Text = s;
                    }
                return s;
            }
        }
        private void cellclick_add()
        {
           int i = nameCod.readCod_int(SELECTprod);
           q.UpdateData(rz.sql.add_list_sc_tehsir_z(i)); 
            
            refresh();
        }
        private void cellclick_edit()
        {
            int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            object y = dataGridView1.SelectedCells[0].Value;
            int yy = dataGridView1.SelectedCells[0].RowIndex;
            object osc = dataGridView1.Rows[yy].Cells[0].Value;
            string codSc = nameCod.readCod(dataGridView1.Rows[yy].Cells[0].Value.ToString());

            string sql = "";

            if (xx == 0) 
            { }  //наименование продукции
            else if (xx == 1) //наименование тех сырья
            {
                DataTable dt = q.selectAny("SELECT cod, name FROM list_tehsir_z;");
                // ETALON.listAny f = 
                //   new ETALON.listAny(dt); f.ShowDialog();

                ETALON.listAnyEdit f =
                new ETALON.listAnyEdit("city", "list_tehsir_z", "cod>0"); f.ShowDialog();
                if (f.IsOK)
                {
                    string s1 = string.Concat(
                        "call `city`.`rz_update_list_sc_tehsir_z`(",
                        f.sCod, " , ",
                        codSc, ");");
                    q.UpdateData(s1);
                    toolStripTextBox1.Text =
                        codSc; refresh();
                }
            }
            
        }
        private void cellclick_del()
        {
            int xx = dataGridView1.SelectedCells[0].ColumnIndex;
            object y = dataGridView1.SelectedCells[0].Value;
            int yy = dataGridView1.SelectedCells[0].RowIndex;
         //   string codSc = nameCod.readCod(dataGridView1.Rows[yy].Cells[2].Value.ToString());
            string codSc = dataGridView1.Rows[yy].Cells[2].Value.ToString();
            q.UpdateData(rz.sql.DEL_FROM_list_sc_tehsir_z(codSc)); refresh();
        }
        private void cellclick_work()
        {
        }
        private void cellclick_non()
        {
        }

        private void toolStripDropDownButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.work);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.add);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.delete);
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            setMode(city7.rzpt.modeWAED.edit);
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cellclick();
        }

        private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //if (toolStripTextBox1.Text.Length > 0)
            refresh(toolStripTextBox1.Text);
        }
    }
}
